<?php


namespace Biz\service\advertisement\position;

use Biz\model\advertisement\MHhzPositionUserprofile;
use Biz\model\advertisement\MPositionAdType;
use Biz\model\advertisement\MPositionRules;
use Biz\service\advertisement\AdUserProfileManagement;
use Doraemon\model\Position\PositionRules;
use Doraemon\service\position\DisplayConstInterface;
use Doraemon\service\position\PositionApi;
use Doraemon\service\position\UserProfileModule;
use Doraemon\tools\HhzEnv;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;

class BizPositionUserprofileGet extends BizPositionPageTpl
{
    const TPL_CONFIG_TPL_NAME = '用户画像应用';


    public function getList(): array
    {
        $condition= $this->getRequestCondition();
        $uid = $condition['uid'] ?? 0;
        $fl = $condition['fl'] ?? [];
        $data = AdUserProfileManagement::getUserProfile($uid, [], $fl);
        $data = $data->toArray();
        $allFields = AdUserProfileManagement::getFieldsMap();
        $response = [];
        foreach ($fl as $v) {
            $response[] = [
              'en' => $v,
              'name' => $allFields[$v],
              'v' => $data[$v] ?? '字段查询结果为空',
            ];
        }
        return $response;
    }

    public function getTotal(): int
    {
        return 0;
    }

    public function condition(): array
    {
        $uid = $this->getRequest()->input('uid', '');
        $fl = $this->getRequest()->input('fl', '');
        $module = $this->getRequest()->input('module', '');
        $ext = $this->getRequest()->input('ext', '');
        $id = $this->getRequest()->input('id', '');
        $condition = [];

        if ($uid) $condition['uid'] = $uid;
        if ($fl) $condition['fl'] = $fl;
        if ($ext) $condition['ext'] = $ext;
        if ($id) $condition['id'] = $id;
        if (is_numeric($module)) $condition['module'] = $module;

        return $condition;
    }

    public function getGrid(Grid $grid)
    {
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disablePagination();
        $grid->disableActions();
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('uid', '用户id');
                $filter->equal('fl', '画像字段')->multipleSelect(AdUserProfileManagement::getFieldsMap());
                $filter->equal('ext', '自定义用户画像')->placeholder('输入格式 uid:110;birthday:20220101');
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('module', '用户画像应用模块')->select(MPositionRules::$moduleMap);
                $filter->equal('id', '配置id')->placeholder('多个id使用 , 分隔');
            });
            $filter->expand();
        });
        $grid->column('en', '字段标识');
        $grid->column('name', '字段名称');
        $grid->column('v', '查询结果');
    }

    public function withInfoHtml(): array
    {
        $condition = $this->getRequestCondition();
        $module = $condition['module'] ?? 0;
        $uid = $condition['uid'] ?? 0;
        $ext = $condition['ext'] ?? ''; //输入格式 uid:110;birthday:20220101
        $ids = $condition['id'] ?? '';
        $ids = explode(',', $ids);
        $userProfileExt = [];
        if ($ext) {
            $arr = explode(";", $ext);
            foreach ($arr as $item) {
                list($key, $val) = explode(":", $item);
                $userProfileExt[$key] = $val;
            }
        }
        UserProfileModule::debug();
        $rs = PositionApi::hitRules($module, $ids, $uid, $userProfileExt);
        $logs = UserProfileModule::debugLogs();

        $html = [];
        $i = 0;
        foreach ($rs as $id => $result) {
            $html[] = "配置id:$id " . ($result ? 'success' : ($logs[$i]));
            $i ++;
        }
        return $html ? ["用户画像匹配结果", implode("<br/>", $html)] : [];
    }

    public function getForm(Form $form)
    {

    }



    /**
     * @throws Exception
     */
    public function save($input)
    {
    }

    public function delete($id): int
    {
        return 0;
    }


    public function getFormInfo($id)
    {

    }
}